package net.m2technologies.open_arm.utilities.generic_object_functions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Copyright 2005 Mark Masterson<br> <br> Licensed under the Apache License, Version 2.0 (the "License");<br> you may
 * not use this file except in compliance with the License.<br> You may obtain a copy of the License at<br> <br>
 * http://www.apache.org/licenses/LICENSE-2.0<br> <br> Unless required by applicable law or agreed to in writing,
 * software<br> distributed under the License is distributed on an "AS IS" BASIS,<br> WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied.<br> See the License for the specific language governing permissions and<br>
 * limitations under the License.<br>
 * <p/>
 * <p>Description: </p>
 *
 * @author Mark Masterson
 * @version 0.010
 */
class PrimitiveObjectArrayWrapperFactory {

    private static final String PACKAGE_NAME_PREFIX
            = new StringBuffer().append(PrimitiveObjectArrayWrapperFactory.class.getPackage().getName()).append('.')
            .toString();

    private final Collection primitiveObjectArrayWrapperNames = new ArrayList();

    PrimitiveObjectArrayWrapperFactory() {
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("BooleanObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("ByteObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("CharacterObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("DoubleObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("FloatObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("IntegerObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("LongObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("ShortObjectArrayWrapper").toString());
        this.primitiveObjectArrayWrapperNames.add(
                new StringBuffer().append(PACKAGE_NAME_PREFIX).append("VoidObjectArrayWrapper").toString());
    }

    AbstractPrimitiveObjectArrayWrapper getInstance(final Object originalObject) {
        final Iterator clazzNames = this.primitiveObjectArrayWrapperNames.iterator();
        Class clazz = null;
        while (clazzNames.hasNext()) {
            try {
                clazz = Class.forName((String) clazzNames.next());
                final AbstractPrimitiveObjectArrayWrapper possibleResult
                        = (AbstractPrimitiveObjectArrayWrapper) clazz.newInstance();
                if (possibleResult.isMyType(originalObject)) {
                    return possibleResult;
                }
            } catch (Exception ex) {
                //move on to the next name in the list...
            }
        }
        return null;
    }
}
